<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('证书申请列表')" />
    <th:block th:include="include :: layout-latest-css" />
    <th:block th:include="include :: ztree-css" />
</head>
<body class="gray-bg">
<div class="ui-layout-west">
    <div class="box box-main">
        <div class="box-header">
            <div class="box-title">
                <i class="fa fa-sitemap"></i> 证书域名列表
            </div>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" id="btnRefresh" title="刷新"><i class="fa fa-refresh"></i></button>
            </div>
        </div>
        <div class="ui-layout-content">
            <div id="tree" class="ztree"></div>
        </div>
    </div>
</div>
<div class="ui-layout-center">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <input type="hidden" name="mainId" id="mainId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>证书域名：</label>
                                <input type="text" name="certificateDomain"/>
                            </li>
                            <li>
                                <label>颁发机构：</label>
                                <select name="issuingAuthority" th:with="type=${@dict.getType('lssuing_authority')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status" th:with="type=${@dict.getType('ssl_status')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="SSL:apply:add">
                    <i class="fa fa-plus"></i> 申请证书
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="SSL:apply:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
            </div>
            <div class="col-sm-12 select-table table-bordered">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
</div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: layout-latest-js" />
    <th:block th:include="include :: ztree-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('SSL:apply:edit')}]];
        var authorizationsFlag = [[${@permission.hasPermi('SSL:apply:authorizations')}]];
        var removeFlag = [[${@permission.hasPermi('SSL:apply:remove')}]];
        var issuingAuthorityDatas = [[${@dict.getType('lssuing_authority')}]];
        var privateKeyTypeDatas = [[${@dict.getType('private_key_type')}]];
        var statusDatas = [[${@dict.getType('ssl_status')}]];
        var prefix = ctx + "SSL/apply";

        function authorizations(id){
            $.modal.customOpen('验证域名所有权', prefix + "/authorizations/"+id, "开始验证", "取消", $(window).width()*0.8);
        }

        function viewSSLContent(id){
            $.modal.openTab('查看下载证书', prefix + "/downFilePage/"+id);
        }

        function downCertificate(id){
            $.operate.post(prefix+"/downCertificate/"+id, null);
        }

        $(function() {
            var panehHidden = false;
            if ($(this).width() < 769) {
                panehHidden = true;
            }
            $('body').layout({ initClosed: panehHidden, west__size: 270, resizeWithWindow: false });
            // 回到顶部绑定
            if ($.fn.toTop !== undefined) {
                var opt = {
                    win:$('.ui-layout-center'),
                    doc:$('.ui-layout-center')
                };
                $('#scroll-up').toTop(opt);
            }
            queryDataList();
            queryServiceTree();
        });

        $('#btnRefresh').click(function() {
            queryServiceTree();
        });

        function queryServiceTree(){
            var url = ctx + "SSL/domain1/queryTree";
            var options = {
                url: url,
                expandLevel: 2,
                onClick : zOnClick
            };
            $.tree.init(options);
            function zOnClick(event, treeId, treeNode) {
                $("#mainId").val(treeNode.id);
                $.table.search();
            }
        }

        function queryDataList() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "证书申请",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '证书Id',
                    visible: false
                },
                {
                    field: 'createTime',
                    title: '申请时间'
                },
                {
                    field: 'issuingAuthority',
                    title: '颁发机构',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(issuingAuthorityDatas, value);
                    }
                },
                {
                    field: 'certificateDomain',
                    title: '证书域名'
                },
                {
                    field: 'privateKeyType',
                    title: '加密算法',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(privateKeyTypeDatas, value);
                    }
                },
                {
                    field: 'daysDifference',
                    title: '到期时间',
                    formatter:function (value, row, index){
                        if(value){
                            if(value > 30){
                                return '<p><span class="badge badge-success">剩余：'+value+' 天</span></p>';
                            }else if (value <=30 && value > 20){
                                return '<p><span class="badge badge-primary">剩余：'+value+' 天</span></p>';
                            }else if (value <=20 && value > 10){
                                return '<p><span class="badge badge-warning">剩余：'+value+' 天</span></p>';
                            }else{
                                return '<p><span class="badge badge-danger">剩余：'+value+' 天</span></p>';
                            }
                        }else{
                            return '<p><span class="badge badge-danger">剩余：0 天</span></p>';
                        }
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.status === '0'){
                            actions.push('<a class="btn btn-success btn-xs ' + authorizationsFlag + '" href="javascript:void(0)" onclick="authorizations(\'' + row.id + '\')"><i class="fa fa-address-card-o"></i>验证</a> ');
                        }else if(row.status === '1'){
                            actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="downCertificate(\'' + row.id + '\')"><i class="fa fa-download"></i>重新签发证书</a> ');
                        }else if(row.status === '3'){
                            actions.push('<a class="btn btn-success btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSSLContent(\'' + row.id + '\')"><i class="fa fa-eye"></i>查看证书</a> ');
                        }
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);
        }
    </script>
</body>
</html>
